package com.lmpop.trade.util;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Random;
import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class CheckImage extends HttpServlet
{
  private int width = 80;

  private int height = 28;

  private int codeCount = 4;

  private int xx = 0;
  private int fontHeight;
  private int codeY;
  char[] codeSequence = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z' };

  public void init()
    throws ServletException
  {
    String strWidth = getInitParameter("width");

    String strHeight = getInitParameter("height");

    String strCodeCount = getInitParameter("codeCount");
    try
    {
      if ((strWidth != null) && (strWidth.length() != 0)) {
        this.width = Integer.parseInt(strWidth);
      }
      if ((strHeight != null) && (strHeight.length() != 0)) {
        this.height = Integer.parseInt(strHeight);
      }
      if ((strCodeCount != null) && (strCodeCount.length() != 0))
        this.codeCount = Integer.parseInt(strCodeCount);
    }
    catch (NumberFormatException e) {
      e.printStackTrace();
    }

    this.xx = (this.width / (this.codeCount + 1));
    this.fontHeight = 25;
    this.codeY = (this.height - 5);
  }

  protected void service(HttpServletRequest req, HttpServletResponse resp)
    throws ServletException, IOException
  {
    BufferedImage buffImg = new BufferedImage(this.width, this.height, 1);

    Graphics2D gd = buffImg.createGraphics();

    Random random = new Random();

    gd.setColor(Color.WHITE);
    gd.fillRect(0, 0, this.width, this.height);

    Font font = new Font("Fixedsys", 0, this.fontHeight);

    gd.setFont(font);

    gd.setColor(Color.BLACK);
    gd.drawRect(0, 0, this.width - 1, this.height - 1);

    gd.setColor(Color.BLACK);
    for (int i = 0; i < 30; i++) {
      int x = random.nextInt(this.width);
      int y = random.nextInt(this.height);
      int xl = random.nextInt(12);
      int yl = random.nextInt(12);
      gd.drawLine(x, y, x + xl, y + yl);
    }

    StringBuffer randomCode = new StringBuffer();
    int red = 0; int green = 0; int blue = 0;

    for (int i = 0; i < this.codeCount; i++)
    {
      String strRand = String.valueOf(this.codeSequence[random.nextInt(26)]);

      red = random.nextInt(255);
      green = random.nextInt(255);
      blue = random.nextInt(255);

      gd.setColor(new Color(red, green, blue));
      gd.drawString(strRand, (i + 1) * this.xx, this.codeY);

      randomCode.append(strRand);
    }

    HttpSession session = req.getSession();
    session.setAttribute("randcode", randomCode.toString());

    resp.setHeader("Pragma", "no-cache");
    resp.setHeader("Cache-Control", "no-cache");
    resp.setDateHeader("Expires", 0L);

    resp.setContentType("image/jpeg");

    ServletOutputStream sos = resp.getOutputStream();
    ImageIO.write(buffImg, "jpeg", sos);
    sos.close();
  }
}